package com.wey.springboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wey.springboot.bean.Grade;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface GradeMapper extends BaseMapper<Grade> {
  @Select("<script>" +
      "select " +
      "   g.*,c.name sourse_name " +
      "from grade g " +
      "   join course c on c.id = g.course_id " +
      "where " +
      "   g.account_id = #{accountId} " +
      "</script>")
  List<Map<String, Object>> searchGrades(Integer id);

  @Select("<script>" +
      "select " +
      "   a.name,a.id " +
      "from account a " +
      "   join grade g on g.account_id = a.id " +
      "group by a.id" +
      "</script>")
  @Results({
      @Result(column = "id", property = "id"),
      @Result(column = "name", property = "name"),
      @Result(property = "grades", column = "id", many = @Many(select = "com.wey.springboot.mapper.GradeMapper.detailMany"))
  })
  List<Map<String, Object>> detail();

  @Select("<script>" +
      "select " +
      "   c.name course_name,c.id course_id, " +
      "   g.id id, g.grade grade " +
      "from grade g " +
      "   join course c on g.course_id = c.id " +
      "where " +
      "   g.account_id = #{id} " +
      "</script>")
  List<Map<String, Object>> detailMany(Integer id);
}
